home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Science
/
RLaB
/
help
/
VARIABLES
< prev
next >
Wrap
Text File
|
1994-04-25
|
4KB
|
98 lines
VARIABLES:
General discussion of variables, including local, static, and
global scoping rules.
Variable names must start with a letter, but can contain
numbers, and underscores ( `_' ) after the 1st character. The
binary and unary arithmetic operators are not allowed in
variable names. RLaB is case sensitive. The variables `A' and
`a' are different. There is no restriction on the length of
variables names. Variable names that start with a `_' will not
show up when who(), or what() are executed, although they do
exist, and can be used like another variable.
A variable can hold any kind of data. Variables hold scalar
and matrix numerics as well as scalar and matrix strings,
lists, built-in functions, and user-functions.
A variable must be initialized, or used on the left-hand-side
of an assignment before it can be otherwise used. Trying to
use an uninitialized variable will result in an `UNDEFINED'
message.
Typing `who()' produces a list of the active variables in the
global workspace. This list includes everything except
builtin and user functions. Typing `what()' will produce a
list of all the active builtin and user functions in the
global workspace.
Assignment to a variable destroys the current contents, and
replaces them with the contents of the right hand side
expression. At present builtin functions cannot be
re-assigned, but user-functions (and any other variable) can
be re-assigned at any time.
The storage space that a variable's data occupies can be
freed for other usage with the clear command (see `help
clear').
-------------------------------------------------------------
There are three scopes that a variable can exist in: local,
file-static, and global.
Global variables are visible to all functions and files, at
all times. Global variable scope is the default in RLaB. This
means that you can access global variables within functions
without special declarations or statements.
Local variables can only exist within functions, and are
declared with the `local' statement. Since global scope is
always the default, local declarations must be made for all
variables that need to be "hidden" from the global
environment. Local variables are created each time the
function is called, and are destroyed each time execution
leaves the function (unless the local variable is returned to
the calling environment). Local variables are UNDEFINED upon
creation - the function must initialize each local variable
before usage.
File-static variables are visible only to other functions, and
statements within the file they are declared in. the static
statement creates variables that will only be seen by other
statements (including functions) within the same file.
File-static variables are created once, and are not destroyed
until the RLaB session is terminated. We will illustrate with
a simple example:
//-------- file count.r -----------------------
static (cnt)
cnt = 0;
//
// Count the number of times something happens
//
count = function ( N )
{
cnt = cnt + N;
return cnt;
};
//--------------------------------------------
In this example the file count.r contains a static variable
and a user-function. The user function simply maintains a sum
of the arguments that count() is called with. During a
particular RLaB session count() will keep track of the sum of
all the values of N. cnt will not appear in the
global-symbol-table. Note that any variable can be file-static
- thus, we can "hide" functions in files by declaring them
static (remember functions are variables).
For a more elaborate example study the file misc/plplot.r in
the original RLaB source distribution. Plplot.r makes
extensive use of file-static variables.